﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using RBS.Core.Extension;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL
{
    //D_Settlement
    public partial class D_Settlement
    { 
        public bool Exists(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from D_Settlement");
            strSql.Append(" where ");
            strSql.Append(" Id = @Id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        } 

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RBS.Core.Model.D_Settlement model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into D_Settlement(");
            strSql.Append("RecAmount,GiveAmount,PayType,PayTime,JF,SendFoodId,SendFood,ConsumptionId,Consumption,AdvertisId,HotelId,Advertis,OrderNo,OrderId,UserId,OrderAmount,Amount,DisAmount,PaidAmount");
            strSql.Append(") values (");
            strSql.Append("@RecAmount,@GiveAmount,@PayType,@PayTime,@JF,@SendFoodId,@SendFood,@ConsumptionId,@Consumption,@AdvertisId,@HotelId,@Advertis,@OrderNo,@OrderId,@UserId,@OrderAmount,@Amount,@DisAmount,@PaidAmount");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@RecAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@GiveAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PayType", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@PayTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@JF", SqlDbType.Int,4) ,            
                        new SqlParameter("@SendFoodId", SqlDbType.Int,4) ,            
                        new SqlParameter("@SendFood", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@ConsumptionId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Consumption", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@AdvertisId", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Advertis", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@OrderNo", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Amount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@DisAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PaidAmount", SqlDbType.Decimal,9)             
              
            };

            parameters[0].Value = model.RecAmount;
            parameters[1].Value = model.GiveAmount;
            parameters[2].Value = model.PayType;
            parameters[3].Value = model.PayTime;
            parameters[4].Value = model.JF;
            parameters[5].Value = model.SendFoodId;
            parameters[6].Value = model.SendFood;
            parameters[7].Value = model.ConsumptionId;
            parameters[8].Value = model.Consumption;
            parameters[9].Value = model.AdvertisId;
            parameters[10].Value = model.HotelId;
            parameters[11].Value = model.Advertis;
            parameters[12].Value = model.OrderNo;
            parameters[13].Value = model.OrderId;
            parameters[14].Value = model.UserId;
            parameters[15].Value = model.OrderAmount;
            parameters[16].Value = model.Amount;
            parameters[17].Value = model.DisAmount;
            parameters[18].Value = model.PaidAmount;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBS.Core.Model.D_Settlement model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update D_Settlement set ");

            strSql.Append(" RecAmount = @RecAmount , ");
            strSql.Append(" GiveAmount = @GiveAmount , ");
            strSql.Append(" PayType = @PayType , ");
            strSql.Append(" PayTime = @PayTime , ");
            strSql.Append(" JF = @JF , ");
            strSql.Append(" SendFoodId = @SendFoodId , ");
            strSql.Append(" SendFood = @SendFood , ");
            strSql.Append(" ConsumptionId = @ConsumptionId , ");
            strSql.Append(" Consumption = @Consumption , ");
            strSql.Append(" AdvertisId = @AdvertisId , ");
            strSql.Append(" HotelId = @HotelId , ");
            strSql.Append(" Advertis = @Advertis , ");
            strSql.Append(" OrderNo = @OrderNo , ");
            strSql.Append(" OrderId = @OrderId , ");
            strSql.Append(" UserId = @UserId , ");
            strSql.Append(" OrderAmount = @OrderAmount , ");
            strSql.Append(" Amount = @Amount , ");
            strSql.Append(" DisAmount = @DisAmount , ");
            strSql.Append(" PaidAmount = @PaidAmount  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@RecAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@GiveAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PayType", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@PayTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@JF", SqlDbType.Int,4) ,            
                        new SqlParameter("@SendFoodId", SqlDbType.Int,4) ,            
                        new SqlParameter("@SendFood", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@ConsumptionId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Consumption", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@AdvertisId", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Advertis", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@OrderNo", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Amount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@DisAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PaidAmount", SqlDbType.Decimal,9)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.RecAmount;
            parameters[2].Value = model.GiveAmount;
            parameters[3].Value = model.PayType;
            parameters[4].Value = model.PayTime;
            parameters[5].Value = model.JF;
            parameters[6].Value = model.SendFoodId;
            parameters[7].Value = model.SendFood;
            parameters[8].Value = model.ConsumptionId;
            parameters[9].Value = model.Consumption;
            parameters[10].Value = model.AdvertisId;
            parameters[11].Value = model.HotelId;
            parameters[12].Value = model.Advertis;
            parameters[13].Value = model.OrderNo;
            parameters[14].Value = model.OrderId;
            parameters[15].Value = model.UserId;
            parameters[16].Value = model.OrderAmount;
            parameters[17].Value = model.Amount;
            parameters[18].Value = model.DisAmount;
            parameters[19].Value = model.PaidAmount;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            var strSql = new StringBuilder();
            strSql.Append("delete from D_Settlement ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from D_Settlement ");
            strSql.Append(" where ID in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        } 
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBS.Core.Model.D_Settlement GetModel(int Id)
        {

            var strSql = new StringBuilder();
            strSql.Append("select Id, RecAmount, GiveAmount, PayType, PayTime, JF, SendFoodId, SendFood, ConsumptionId, Consumption, AdvertisId, HotelId, Advertis, OrderNo, OrderId, UserId, OrderAmount, Amount, DisAmount, PaidAmount  ");
            strSql.Append("  from D_Settlement ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            RBS.Core.Model.D_Settlement model = new RBS.Core.Model.D_Settlement();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                model = ds.Tables[0].Rows[0].ConvertToEntity<RBS.Core.Model.D_Settlement>();
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM D_Settlement ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM D_Settlement ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * FROM D_Settlement ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion  ExtensionMethod


    }
}

